package com.wmyg.antifake.service.impl;

import com.wmyg.antifake.entity.BkUser;
import com.wmyg.antifake.entity.NoticePolicy;
import com.wmyg.antifake.entity.Product;
import com.wmyg.antifake.entity.ProductOrigin;
import com.wmyg.antifake.fabric.FabricDao;
import com.wmyg.antifake.fabric.dto.ProductFabricDto;
import com.wmyg.antifake.repository.BkUserRepository;
import com.wmyg.antifake.repository.ProductRepository;
import com.wmyg.antifake.service.ProductService;
import com.wmyg.antifake.utils.ProductHashUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 王卓逸
 * @program TODO
 * @description TODO
 * @create 2019-05-08
 */
@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    BkUserRepository bkUserRepository;

    @Autowired
    ProductRepository productRepository;

    @Autowired
    FabricDao fabricDao;

    @Override
    public Boolean saveProduct(Product product, String username) {
        BkUser bkUser = bkUserRepository.findByUsername(username);
        if (null != bkUser && null != bkUser.getCompany()) {
            product.setCompany(bkUser.getCompany());
            Product productOld = productRepository.findByProductId(product.getProductId());
            if (null == productOld || null == productOld.getPolicies() || productOld.getPolicies().isEmpty()) {
                NoticePolicy noticePolicy = new NoticePolicy();
                noticePolicy.setPolicy(0);
                noticePolicy.setNormalContent("属于正常范围");
                noticePolicy.setWarningValue(5);
                noticePolicy.setWarningContent("查询次数过多，请谨防假冒");
                noticePolicy.setDangerValue(10);
                noticePolicy.setDangerContent("查询次数过多，请谨防假冒");
                product.getPolicies().add(noticePolicy);
            } else {
                product.getPolicies().addAll(productOld.getPolicies());
            }
            if (null != productOld) {
                product.setCreateTime(productOld.getCreateTime());
            }
            productRepository.save(product);
            product = productRepository.findByProductId(product.getProductId());
            String productHash = ProductHashUtil.hashProduct(product);
            fabricDao.invoke("setProductHash", new ProductFabricDto(product, productHash));
            return true;
        } else {
            return false;
        }
    }

    @Override
    public List<Product> getProductList(String username) {
        BkUser bkUser = bkUserRepository.findByUsername(username);
        if (null != bkUser && null != bkUser.getCompany()) {
            return productRepository.findByCompanyOrderByCreateTimeDesc(bkUser.getCompany());
        } else {
            return null;
        }
    }

    @Override
    public Page<Product> getProductPage(String username, Integer index) {
        BkUser bkUser = bkUserRepository.findByUsername(username);
        if (null != bkUser && null != bkUser.getCompany()) {
            return productRepository.findByCompanyOrderByCreateTimeDesc(PageRequest.of(index, 10), bkUser.getCompany());
        } else {
            return null;
        }
    }

    @Override
    public Boolean updateProduct(Product product, String username) {
        BkUser bkUser = bkUserRepository.findByUsername(username);
        if (null != bkUser && null != bkUser.getCompany()) {
            product.setCompany(bkUser.getCompany());
            Product productOld = productRepository.findByProductId(product.getProductId());
            product.getPolicies().addAll(productOld.getPolicies());
            product.setCreateTime(productOld.getCreateTime());
            for (ProductOrigin origin :
                    product.getProductOrigins()) {
                for (ProductOrigin o :
                        productOld.getProductOrigins()) {
                    if (origin.getOriginInfo().equals(o.getOriginInfo())) {
                        origin.setOriginId(o.getOriginId());
                    }
                }
            }
            productRepository.save(product);
            product = productRepository.findByProductId(product.getProductId());
            String productHash = ProductHashUtil.hashProduct(product);
            fabricDao.invoke("setProductHash", new ProductFabricDto(product, productHash));
            return true;
        } else {
            return false;
        }
    }

    @Override
    public Product findByProductId(String productId) {
        return productRepository.findByProductId(productId);
    }

    @Override
    public Page<Product> getProductPage(String username, Integer index, String somethingLike) {
        String s = "%" + somethingLike + "%";
        BkUser bkUser = bkUserRepository.findByUsername(username);
        if (null != bkUser && null != bkUser.getCompany()) {
            return productRepository.findByProductIdLikeOrProductNameLikeAndCompanyOrderByCreateTimeDesc(PageRequest.of(index, 10), s, s, bkUser.getCompany());
        } else {
            return null;
        }
    }
}
